package cn.itcast.up.common.mytest

import java.io.FileOutputStream
import java.util.Properties

import cn.itcast.up.common.{ConfigHolder, HdfsTools}
import com.typesafe.config.{Config, ConfigFactory}
import org.apache.oozie.client.OozieClient

/**
 * 自己实现作业的提交到oozie功能
 */
object MyOozieUtils {

  // 获取类的加载器对象
  private val loader: ClassLoader = MyOozieUtils.getClass.getClassLoader


  def main(args: Array[String]): Unit = {
    val props = new Properties()
    // 向props中添加信息
    // 先添加配置文件中已有的信息
    val params: Map[String, String] = ConfigHolder.oozie.params
    // 遍历map
    for ((k, v) <- params) {
      props.setProperty(k, v)
    }
    // 再添加jar路径/class路径/调度时间等信息.
    props.setProperty("start", "2019-09-01T01:00+0800")
    props.setProperty("end", "2030-09-01T01:00+0800")
    props.setProperty("mainClass", "cn.itcast.up29.TestTag")
    props.setProperty("jarPath", "hdfs://bd001:8020/apps/tags/models/Tag_001/lib/model29.jar")
    props.setProperty("appPath", "hdfs://bd001:8020/apps/tags_new/models/tags_19")
    props.setProperty(OozieClient.COORDINATOR_APP_PATH, "hdfs://bd001:8020/apps/tags_new/models/tags_19")
    // 保存props中的配置信息
    props.store(new FileOutputStream(loader.getResource("oozie/job.properties").getPath), "")

    // 将配置文件上传到HDFS
    //coordinator.xml workflow.xml job.properties

    val coordinatorPath: String = loader.getResource("oozie/coordinator.xml").getPath
    val workflowPath: String = loader.getResource("oozie/workflow.xml").getPath
    val propsPath: String = loader.getResource("oozie/job.properties").getPath

    HdfsTools.build().uploadLocalFile2HDFS(coordinatorPath, "hdfs://bd001:8020/apps/tags_new/models/tags_19")
    HdfsTools.build().uploadLocalFile2HDFS(workflowPath, "hdfs://bd001:8020/apps/tags_new/models/tags_19")
    HdfsTools.build().uploadLocalFile2HDFS(propsPath, "hdfs://bd001:8020/apps/tags_new/models/tags_19")


    // 创建一个Oozie客户端对象
    val client = new OozieClient("http://bd001:11000/oozie")
    // 指定配置文件并运行.
    val id: String = client.run(props)
    println("当前作业的ID: " + id)

    //    client.kill(id) // 停止作业

  }
}
